package carassius.GUI;

import carassius.BLL.Settings;
import carassius.DAL.AnimalRow;
import carassius.DAL.Database;
import carassius.DAL.OptimalOrganismWaterConditionRow;
import carassius.DAL.PictureRow;
import carassius.DAL.WaterConditionRow;
import java.awt.Window;
import java.io.File;
import java.sql.SQLException;
import java.util.Collection;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author siebz0r
 */
public class AnimalForm
		extends javax.swing.JDialog
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private AnimalRow _animalRow;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public AnimalForm(Window parent, AnimalRow animalRow)
	{
		super(parent, ModalityType.APPLICATION_MODAL);
		_animalRow = animalRow;
		initComponents();
		fillTableWithWaterConditions();
		fillFormByAnimalRow(_animalRow);
	}

	public AnimalForm(Window parent)
	{
		this(parent, null);
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Private Methods">
	private void fillTableWithWaterConditions()
	{
		try
		{
			Collection<WaterConditionRow> waterConditionRows = Database.getWaterConditionTable().getAll();
			DefaultTableModel model = (DefaultTableModel) uxOptimalWaterValuesTable.getModel();
			model.setRowCount(0);
			for (WaterConditionRow row : waterConditionRows)
			{
				model.addRow(new Object[]
						{
							row, null, null
						});
			}
		}
		catch (SQLException ex)
		{
			uxSaveButton.setEnabled(false);
			JOptionPane.showMessageDialog(this,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("FetchWaterConditionsError"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("FetchWaterConditionsErrorTitle"),
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void fillFormByAnimalRow(AnimalRow animalRow)
	{
		if (animalRow instanceof AnimalRow)
		{
			uxScientificNameTextField.setText(animalRow.getScientificName());
			uxAlternativeNameTextField.setText(animalRow.getAlternativeName());
			uxDescriptionTextArea.setText(animalRow.getDescription());
			uxMinTemperatureIntegerTextField.setValue(animalRow.getMinTemperature());
			uxMaxTemperatureIntegerTextField.setValue(animalRow.getMaxtemperature());
			uxMaxLengthDoubleTextField.setValue(animalRow.getMaxSize());
			if (animalRow.getPicture() != null)
			{
				File pic = new File(animalRow.getPicture().getPath());
				if (pic.exists())
				{
					uxPicturePanel.setImagePath(animalRow.getPicture().getPath());
				}
			}
			uxFoodTextArea.setText(animalRow.getFeed());
			uxMinAquariumLengthIntegerTextField.setValue(animalRow.getAquariumLength());
			uxMinAquariumHeightIntegerTextField.setValue(animalRow.getAquariumHeight());
			uxMinAquariumLitersPerFishIntegerTextField.setValue(animalRow.getAquariumLiter());
			uxBreedingInformationTextArea.setText(animalRow.getBreed());
			uxOriginTextField.setText(animalRow.getOrigin());
			uxSchoolingCheckBox.setSelected(animalRow.isSchooling());

			try
			{
				Collection<OptimalOrganismWaterConditionRow> optimalWaterConditionRows = Database.getOptimalOrganismWaterConditionTable().getByOrganismId(animalRow.getId());
				DefaultTableModel model = (DefaultTableModel) uxOptimalWaterValuesTable.getModel();
				for (OptimalOrganismWaterConditionRow row : optimalWaterConditionRows)
				{
					for (int i = 0; i < model.getRowCount(); i++)
					{
						if (row.getCondition().equals(model.getValueAt(i, 0)))
						{
							model.setValueAt(row, i, 0);
							model.setValueAt(row.getMinValue(), i, 1);
							model.setValueAt(row.getMaxValue(), i, 2);
						}
					}
				}
			}
			catch (SQLException ex)
			{
				uxSaveButton.setEnabled(false);
				JOptionPane.showMessageDialog(this,
						ResourceBundle.getBundle("carassius.resources.Messages").getString("FetchWaterConditionsError"),
						ResourceBundle.getBundle("carassius.resources.Messages").getString("FetchWaterConditionsErrorTitle"),
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	private boolean isInputValid()
	{
		return (uxMinTemperatureIntegerTextField.isValid()
				&& uxMaxTemperatureIntegerTextField.isValid()
				&& uxMinAquariumHeightIntegerTextField.isValid()
				&& uxMinAquariumLengthIntegerTextField.isValid()
				&& uxMinAquariumLitersPerFishIntegerTextField.isValid()
				&& uxMaxLengthDoubleTextField.isValid()
				&& uxOptimalWaterValuesTable.isInputValid());
	}

	private boolean nameSuplied()
	{
		String scientificName = uxScientificNameTextField.getText();
		String alternativeName = uxAlternativeNameTextField.getText();
		return !((scientificName == null || scientificName.isEmpty())
				&& (alternativeName == null || alternativeName.isEmpty()));
	}

	private PictureRow getPictureRow()
	{
		String imagePath = uxPicturePanel.getImagePath();
		if (imagePath != null
				&& !imagePath.isEmpty())
		{
			try
			{
				PictureRow pictureRow = Database.getPictureTable().getByPath(imagePath);
				if (pictureRow == null)
				{
					pictureRow = Database.getPictureTable().add(imagePath, null, null);
				}
				return pictureRow;
			}
			catch (SQLException ex)
			{
				System.out.println(ex.toString());
			}
		}
		return null;
	}

	private boolean saveNewFish()
	{
		String scientificName = uxScientificNameTextField.getText();
		String alternativeName = uxAlternativeNameTextField.getText();
		String description = uxDescriptionTextArea.getText();
		String feed = uxFoodTextArea.getText();
		String breed = uxBreedingInformationTextArea.getText();
		String origin = uxOriginTextField.getText();

		try
		{
			_animalRow = (AnimalRow) Database.getAnimalTable().add(
					(!scientificName.isEmpty()) ? scientificName : null,
					(!alternativeName.isEmpty()) ? alternativeName : null,
					(!description.isEmpty()) ? description : null,
					uxMinTemperatureIntegerTextField.getValue(),
					uxMaxTemperatureIntegerTextField.getValue(),
					uxMaxLengthDoubleTextField.getValue(),
					getPictureRow(),
					(!feed.isEmpty()) ? feed : null,
					uxMinAquariumLengthIntegerTextField.getValue(),
					uxMinAquariumHeightIntegerTextField.getValue(),
					uxMinAquariumLitersPerFishIntegerTextField.getValue(),
					(!breed.isEmpty()) ? breed : null,
					(!origin.isEmpty()) ? origin : null,
					uxSchoolingCheckBox.isSelected());
			if (_animalRow != null)
			{
				return true;
			}
			return true;
		}
		catch (SQLException ex)
		{
			System.out.println(ex.toString());
		}
		return false;
	}

	private boolean updateFish()
	{
		String scientificName = uxScientificNameTextField.getText();
		String alternativeName = uxAlternativeNameTextField.getText();
		String description = uxDescriptionTextArea.getText();
		String feed = uxFoodTextArea.getText();
		String breed = uxBreedingInformationTextArea.getText();
		String origin = uxOriginTextField.getText();

		_animalRow.setScientificName((!scientificName.isEmpty()) ? scientificName : null);
		_animalRow.setAlternativeName((!alternativeName.isEmpty()) ? alternativeName : null);
		_animalRow.setDescription((!description.isEmpty()) ? description : null);
		_animalRow.setMinTemperature(uxMinTemperatureIntegerTextField.getValue());
		_animalRow.setMaxtemperature(uxMaxTemperatureIntegerTextField.getValue());
		_animalRow.setMaxSize(uxMaxLengthDoubleTextField.getValue());
		_animalRow.setPicture(getPictureRow());
		_animalRow.setFeed((!feed.isEmpty()) ? feed : null);
		_animalRow.setAquariumLength(uxMinAquariumLengthIntegerTextField.getValue());
		_animalRow.setAquariumHeight(uxMinAquariumHeightIntegerTextField.getValue());
		_animalRow.setAquariumLiter(uxMinAquariumLitersPerFishIntegerTextField.getValue());
		_animalRow.setBreed((!breed.isEmpty()) ? breed : null);
		_animalRow.setOrigin((!origin.isEmpty()) ? origin : null);
		_animalRow.setSchooling(uxSchoolingCheckBox.isSelected());
		return true;
	}

	private boolean saveWaterValues()
	{
		DefaultTableModel model = (DefaultTableModel) uxOptimalWaterValuesTable.getModel();
		for (int i = 0; i < model.getRowCount(); i++)
		{
			Double min = (Double) model.getValueAt(i, 1);
			Double max = (Double) model.getValueAt(i, 2);

			if ((min != null || max != null)
					&& model.getValueAt(i, 0) instanceof WaterConditionRow)
			{
				WaterConditionRow row = (WaterConditionRow) model.getValueAt(i, 0);
				try
				{
					OptimalOrganismWaterConditionRow newOptimalOrganismWaterConditionRow = Database.getOptimalOrganismWaterConditionTable().add(_animalRow, row, min, max);
					if (newOptimalOrganismWaterConditionRow != null)
					{
						model.setValueAt(newOptimalOrganismWaterConditionRow, i, 0);
					}
					else
					{
						return false;
					}
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
					return false;
				}
			}
			else if (model.getValueAt(i, 0) instanceof OptimalOrganismWaterConditionRow)
			{
				OptimalOrganismWaterConditionRow row = (OptimalOrganismWaterConditionRow) model.getValueAt(i, 0);
				if (!updateOptimalWaterValue(row, min, max))
				{
					return false;
				}
			}
		}
		return true;
	}

	private boolean updateOptimalWaterValue(OptimalOrganismWaterConditionRow row,
											Double min, Double max)
	{
		if (min == null && max == null)
		{
			try
			{
				return Database.getOptimalOrganismWaterConditionTable().delete(row);
			}
			catch (SQLException ex)
			{
				System.out.println(ex.toString());
				return false;
			}
		}
		else
		{
			row.setMinValue(min);
			row.setMaxValue(max);
			return true;
		}
	}
	//</editor-fold>

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        uxPicturePanel = new carassius.GUI.PicturePanel();
        uxMinAquariumSizePanel = new javax.swing.JPanel();
        uxMinAquariumLengthPrefixLabel = new javax.swing.JLabel();
        uxMinAquariumHeightPrefixLabel = new javax.swing.JLabel();
        uxMinAquariumLitersPerFishPrefixLabel = new javax.swing.JLabel();
        uxMinAquariumLengthIntegerTextField = new carassius.GUI.IntegerTextField();
        uxMinAquariumHeightIntegerTextField = new carassius.GUI.IntegerTextField();
        uxMinAquariumLitersPerFishIntegerTextField = new carassius.GUI.IntegerTextField();
        uxButtonPanel = new javax.swing.JPanel();
        uxSaveButton = new javax.swing.JButton();
        uxCloseButton = new javax.swing.JButton();
        uxMainInfoPanel = new javax.swing.JPanel();
        uxAlternativeNameTextField = new TextFieldWithMenu();
        uxOriginTextField = new TextFieldWithMenu();
        uxScientificNameTextField = new TextFieldWithMenu();
        uxTemperaturePrefixLabel = new javax.swing.JLabel();
        uxAlternativeNamePrefixLabel = new javax.swing.JLabel();
        uxMaxLengthPrefixLabel = new javax.swing.JLabel();
        uxOriginPrefixLabel = new javax.swing.JLabel();
        uxScientificNamePrefixLabel = new javax.swing.JLabel();
        uxMaxLengthPanel = new javax.swing.JPanel();
        uxMaxLengthDoubleTextField = new carassius.GUI.DoubleTextField();
        jLabel1 = new javax.swing.JLabel();
        uxTemperaturePanel = new javax.swing.JPanel();
        uxMinTemperatureIntegerTextField = new carassius.GUI.IntegerTextField();
        uxTemperatureLabel = new javax.swing.JLabel();
        uxMaxTemperatureIntegerTextField = new carassius.GUI.IntegerTextField();
        uxSchoolingCheckBox = new javax.swing.JCheckBox();
        uxMidInfoPanel = new javax.swing.JPanel();
        uxFoodPanel = new javax.swing.JPanel();
        uxFoodScrollPane = new javax.swing.JScrollPane();
        uxFoodTextArea = new TextAreaWithMenu();
        uxDescriptionPanel = new javax.swing.JPanel();
        uxDescriptionScrollPane = new javax.swing.JScrollPane();
        uxDescriptionTextArea = new TextAreaWithMenu();
        uxBreedingInformationPanel = new javax.swing.JPanel();
        uxBreedingInformationScrollPane = new javax.swing.JScrollPane();
        uxBreedingInformationTextArea = new TextAreaWithMenu();
        uxOptimalWaterConditionsPanel = new javax.swing.JPanel();
        uxOptimalWaterValuesScrollPane = new javax.swing.JScrollPane();
        uxOptimalWaterValuesTable = new carassius.GUI.WaterValuesTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(java.util.ResourceBundle.getBundle("carassius/GUI/resources/AnimalForm").getString((_animalRow == null) ? "Form.title.new" : "Form.title.update"));
        setMinimumSize(new java.awt.Dimension(575, 565));
        setName("Form"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        uxPicturePanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        uxPicturePanel.setMaximumSize(new java.awt.Dimension(150, 150));
        uxPicturePanel.setMinimumSize(new java.awt.Dimension(150, 150));
        uxPicturePanel.setName("uxPicturePanel"); // NOI18N
        uxPicturePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                uxPicturePanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout uxPicturePanelLayout = new javax.swing.GroupLayout(uxPicturePanel);
        uxPicturePanel.setLayout(uxPicturePanelLayout);
        uxPicturePanelLayout.setHorizontalGroup(
            uxPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 146, Short.MAX_VALUE)
        );
        uxPicturePanelLayout.setVerticalGroup(
            uxPicturePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 146, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(uxPicturePanel, gridBagConstraints);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(carassius.CarassiusApp.class).getContext().getResourceMap(AnimalForm.class);
        uxMinAquariumSizePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("uxMinAquariumSizePanel.border.title"))); // NOI18N
        uxMinAquariumSizePanel.setName("uxMinAquariumSizePanel"); // NOI18N
        uxMinAquariumSizePanel.setLayout(new java.awt.GridBagLayout());

        uxMinAquariumLengthPrefixLabel.setText(resourceMap.getString("uxMinAquariumLengthPrefixLabel.text")); // NOI18N
        uxMinAquariumLengthPrefixLabel.setName("uxMinAquariumLengthPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        uxMinAquariumSizePanel.add(uxMinAquariumLengthPrefixLabel, gridBagConstraints);

        uxMinAquariumHeightPrefixLabel.setText(resourceMap.getString("uxMinAquariumHeightPrefixLabel.text")); // NOI18N
        uxMinAquariumHeightPrefixLabel.setName("uxMinAquariumHeightPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxMinAquariumSizePanel.add(uxMinAquariumHeightPrefixLabel, gridBagConstraints);

        uxMinAquariumLitersPerFishPrefixLabel.setText(resourceMap.getString("uxMinAquariumLitersPerFishPrefixLabel.text")); // NOI18N
        uxMinAquariumLitersPerFishPrefixLabel.setName("uxMinAquariumLitersPerFishPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        uxMinAquariumSizePanel.add(uxMinAquariumLitersPerFishPrefixLabel, gridBagConstraints);

        uxMinAquariumLengthIntegerTextField.setName("uxMinAquariumLengthIntegerTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        uxMinAquariumSizePanel.add(uxMinAquariumLengthIntegerTextField, gridBagConstraints);

        uxMinAquariumHeightIntegerTextField.setName("uxMinAquariumHeightIntegerTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        uxMinAquariumSizePanel.add(uxMinAquariumHeightIntegerTextField, gridBagConstraints);

        uxMinAquariumLitersPerFishIntegerTextField.setName("uxMinAquariumLitersPerFishIntegerTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        uxMinAquariumSizePanel.add(uxMinAquariumLitersPerFishIntegerTextField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 70;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(uxMinAquariumSizePanel, gridBagConstraints);

        uxButtonPanel.setName("uxButtonPanel"); // NOI18N
        uxButtonPanel.setLayout(new java.awt.GridBagLayout());

        uxSaveButton.setText(resourceMap.getString("uxSaveButton.text")); // NOI18N
        uxSaveButton.setName("uxSaveButton"); // NOI18N
        uxSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxSaveButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxButtonPanel.add(uxSaveButton, gridBagConstraints);

        uxCloseButton.setText(resourceMap.getString("uxCloseButton.text")); // NOI18N
        uxCloseButton.setName("uxCloseButton"); // NOI18N
        uxCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uxCloseButtonActionPerformed(evt);
            }
        });
        uxButtonPanel.add(uxCloseButton, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        getContentPane().add(uxButtonPanel, gridBagConstraints);

        uxMainInfoPanel.setName("uxMainInfoPanel"); // NOI18N
        uxMainInfoPanel.setLayout(new java.awt.GridBagLayout());

        uxAlternativeNameTextField.setText(resourceMap.getString("uxAlternativeNameTextField.text")); // NOI18N
        uxAlternativeNameTextField.setName("uxAlternativeNameTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxAlternativeNameTextField, gridBagConstraints);

        uxOriginTextField.setText(resourceMap.getString("uxOriginTextField.text")); // NOI18N
        uxOriginTextField.setName("uxOriginTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxOriginTextField, gridBagConstraints);

        uxScientificNameTextField.setText(resourceMap.getString("uxScientificNameTextField.text")); // NOI18N
        uxScientificNameTextField.setName("uxScientificNameTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        uxMainInfoPanel.add(uxScientificNameTextField, gridBagConstraints);

        uxTemperaturePrefixLabel.setText(resourceMap.getString("uxTemperaturePrefixLabel.text")); // NOI18N
        uxTemperaturePrefixLabel.setName("uxTemperaturePrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxTemperaturePrefixLabel, gridBagConstraints);

        uxAlternativeNamePrefixLabel.setText(resourceMap.getString("uxAlternativeNamePrefixLabel.text")); // NOI18N
        uxAlternativeNamePrefixLabel.setName("uxAlternativeNamePrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxAlternativeNamePrefixLabel, gridBagConstraints);

        uxMaxLengthPrefixLabel.setText(resourceMap.getString("uxMaxLengthPrefixLabel.text")); // NOI18N
        uxMaxLengthPrefixLabel.setName("uxMaxLengthPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxMaxLengthPrefixLabel, gridBagConstraints);

        uxOriginPrefixLabel.setText(resourceMap.getString("uxOriginPrefixLabel.text")); // NOI18N
        uxOriginPrefixLabel.setName("uxOriginPrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxOriginPrefixLabel, gridBagConstraints);

        uxScientificNamePrefixLabel.setText(resourceMap.getString("uxScientificNamePrefixLabel.text")); // NOI18N
        uxScientificNamePrefixLabel.setName("uxScientificNamePrefixLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        uxMainInfoPanel.add(uxScientificNamePrefixLabel, gridBagConstraints);

        uxMaxLengthPanel.setName("uxMaxLengthPanel"); // NOI18N
        uxMaxLengthPanel.setLayout(new javax.swing.BoxLayout(uxMaxLengthPanel, javax.swing.BoxLayout.LINE_AXIS));

        uxMaxLengthDoubleTextField.setName("uxMaxLengthDoubleTextField"); // NOI18N
        uxMaxLengthPanel.add(uxMaxLengthDoubleTextField);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        uxMaxLengthPanel.add(jLabel1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxMaxLengthPanel, gridBagConstraints);

        uxTemperaturePanel.setName("uxTemperaturePanel"); // NOI18N
        uxTemperaturePanel.setLayout(new javax.swing.BoxLayout(uxTemperaturePanel, javax.swing.BoxLayout.LINE_AXIS));

        uxMinTemperatureIntegerTextField.setName("uxMinTemperatureIntegerTextField"); // NOI18N
        uxTemperaturePanel.add(uxMinTemperatureIntegerTextField);

        uxTemperatureLabel.setFont(resourceMap.getFont("uxTemperatureLabel.font")); // NOI18N
        uxTemperatureLabel.setText(resourceMap.getString("uxTemperatureLabel.text")); // NOI18N
        uxTemperatureLabel.setName("uxTemperatureLabel"); // NOI18N
        uxTemperaturePanel.add(uxTemperatureLabel);

        uxMaxTemperatureIntegerTextField.setName("uxMaxTemperatureIntegerTextField"); // NOI18N
        uxTemperaturePanel.add(uxMaxTemperatureIntegerTextField);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        uxMainInfoPanel.add(uxTemperaturePanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.6;
        getContentPane().add(uxMainInfoPanel, gridBagConstraints);

        uxSchoolingCheckBox.setText(resourceMap.getString("uxSchoolingCheckBox.text")); // NOI18N
        uxSchoolingCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        uxSchoolingCheckBox.setName("uxSchoolingCheckBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(uxSchoolingCheckBox, gridBagConstraints);

        uxMidInfoPanel.setName("uxMidInfoPanel"); // NOI18N
        uxMidInfoPanel.setLayout(new java.awt.GridBagLayout());

        uxFoodPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("uxFoodPanel.border.title"))); // NOI18N
        uxFoodPanel.setName("uxFoodPanel"); // NOI18N
        uxFoodPanel.setLayout(new java.awt.CardLayout());

        uxFoodScrollPane.setName("uxFoodScrollPane"); // NOI18N

        uxFoodTextArea.setColumns(20);
        uxFoodTextArea.setLineWrap(true);
        uxFoodTextArea.setRows(5);
        uxFoodTextArea.setName("uxFoodTextArea"); // NOI18N
        uxFoodScrollPane.setViewportView(uxFoodTextArea);

        uxFoodPanel.add(uxFoodScrollPane, "card2");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxMidInfoPanel.add(uxFoodPanel, gridBagConstraints);

        uxDescriptionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("uxDescriptionPanel.border.title"))); // NOI18N
        uxDescriptionPanel.setName("uxDescriptionPanel"); // NOI18N
        uxDescriptionPanel.setLayout(new java.awt.CardLayout());

        uxDescriptionScrollPane.setName("uxDescriptionScrollPane"); // NOI18N

        uxDescriptionTextArea.setColumns(20);
        uxDescriptionTextArea.setLineWrap(true);
        uxDescriptionTextArea.setRows(5);
        uxDescriptionTextArea.setName("uxDescriptionTextArea"); // NOI18N
        uxDescriptionScrollPane.setViewportView(uxDescriptionTextArea);

        uxDescriptionPanel.add(uxDescriptionScrollPane, "card2");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxMidInfoPanel.add(uxDescriptionPanel, gridBagConstraints);

        uxBreedingInformationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("uxBreedingInformationPanel.border.title"))); // NOI18N
        uxBreedingInformationPanel.setName("uxBreedingInformationPanel"); // NOI18N
        uxBreedingInformationPanel.setLayout(new java.awt.CardLayout());

        uxBreedingInformationScrollPane.setName("uxBreedingInformationScrollPane"); // NOI18N

        uxBreedingInformationTextArea.setColumns(20);
        uxBreedingInformationTextArea.setLineWrap(true);
        uxBreedingInformationTextArea.setRows(5);
        uxBreedingInformationTextArea.setName("uxBreedingInformationTextArea"); // NOI18N
        uxBreedingInformationScrollPane.setViewportView(uxBreedingInformationTextArea);

        uxBreedingInformationPanel.add(uxBreedingInformationScrollPane, "card2");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        uxMidInfoPanel.add(uxBreedingInformationPanel, gridBagConstraints);

        uxOptimalWaterConditionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("uxOptimalWaterConditionsPanel.border.title"))); // NOI18N
        uxOptimalWaterConditionsPanel.setName("uxOptimalWaterConditionsPanel"); // NOI18N
        uxOptimalWaterConditionsPanel.setLayout(new javax.swing.BoxLayout(uxOptimalWaterConditionsPanel, javax.swing.BoxLayout.LINE_AXIS));

        uxOptimalWaterValuesScrollPane.setName("uxOptimalWaterValuesScrollPane"); // NOI18N

        uxOptimalWaterValuesTable.setName("uxOptimalWaterValuesTable"); // NOI18N
        uxOptimalWaterValuesScrollPane.setViewportView(uxOptimalWaterValuesTable);

        uxOptimalWaterConditionsPanel.add(uxOptimalWaterValuesScrollPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        uxMidInfoPanel.add(uxOptimalWaterConditionsPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.6;
        gridBagConstraints.weighty = 0.6000000000000001;
        getContentPane().add(uxMidInfoPanel, gridBagConstraints);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-570)/2, (screenSize.height-590)/2, 570, 590);
    }// </editor-fold>//GEN-END:initComponents

	private void uxPicturePanelMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_uxPicturePanelMouseClicked
	{//GEN-HEADEREND:event_uxPicturePanelMouseClicked
		String lastPictureLocation = Settings.getSetting("LastPictureLocation");
		JFileChooser fc = new JFileChooser(lastPictureLocation);
		fc.setFileFilter(new FileNameExtensionFilter(ResourceBundle.getBundle("carassius/resources/ObjectNames").getString("Images"), "jpg", "jpeg", "png", "gif"));
		fc.setMultiSelectionEnabled(false);
		PicturePanel picturePanel = new PicturePanel();
		fc.setAccessory(picturePanel);
		fc.addPropertyChangeListener(picturePanel);
		if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
		{
			File image = fc.getSelectedFile();
			uxPicturePanel.setImagePath(image);
			lastPictureLocation = (fc.getCurrentDirectory() != null) ? fc.getCurrentDirectory().getPath() : null;
			Settings.setSetting("LastPictureLocation", lastPictureLocation);
		}
	}//GEN-LAST:event_uxPicturePanelMouseClicked

	private void uxSaveButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxSaveButtonActionPerformed
	{//GEN-HEADEREND:event_uxSaveButtonActionPerformed
		if (nameSuplied())
		{
			if (isInputValid())
			{
				boolean fishSaved = false;
				if (_animalRow != null)
				{
					fishSaved = updateFish();
				}
				else
				{
					fishSaved = saveNewFish();
				}

				if (fishSaved && saveWaterValues())
				{
					JOptionPane.showMessageDialog(this,
							ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalSaved"),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalSavedTitle"),
							JOptionPane.INFORMATION_MESSAGE);
					dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,
							ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalSaveError"),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalSaveErrorTitle"),
							JOptionPane.ERROR_MESSAGE);
				}
			}
			else
			{
				JOptionPane.showMessageDialog(this,
						ResourceBundle.getBundle("carassius.resources.Messages").getString("GenericWrongInputError"),
						ResourceBundle.getBundle("carassius.resources.Messages").getString("GenericWrongInputErrorTitle"),
						JOptionPane.ERROR_MESSAGE);
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalNameNotSuppliedError"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("AnimalNameNotSuppliedErrorTitle"),
					JOptionPane.WARNING_MESSAGE);
		}
	}//GEN-LAST:event_uxSaveButtonActionPerformed

	private void uxCloseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_uxCloseButtonActionPerformed
	{//GEN-HEADEREND:event_uxCloseButtonActionPerformed
		dispose();
	}//GEN-LAST:event_uxCloseButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel uxAlternativeNamePrefixLabel;
    private javax.swing.JTextField uxAlternativeNameTextField;
    private javax.swing.JPanel uxBreedingInformationPanel;
    private javax.swing.JScrollPane uxBreedingInformationScrollPane;
    private javax.swing.JTextArea uxBreedingInformationTextArea;
    private javax.swing.JPanel uxButtonPanel;
    private javax.swing.JButton uxCloseButton;
    private javax.swing.JPanel uxDescriptionPanel;
    private javax.swing.JScrollPane uxDescriptionScrollPane;
    private javax.swing.JTextArea uxDescriptionTextArea;
    private javax.swing.JPanel uxFoodPanel;
    private javax.swing.JScrollPane uxFoodScrollPane;
    private javax.swing.JTextArea uxFoodTextArea;
    private javax.swing.JPanel uxMainInfoPanel;
    private carassius.GUI.DoubleTextField uxMaxLengthDoubleTextField;
    private javax.swing.JPanel uxMaxLengthPanel;
    private javax.swing.JLabel uxMaxLengthPrefixLabel;
    private carassius.GUI.IntegerTextField uxMaxTemperatureIntegerTextField;
    private javax.swing.JPanel uxMidInfoPanel;
    private carassius.GUI.IntegerTextField uxMinAquariumHeightIntegerTextField;
    private javax.swing.JLabel uxMinAquariumHeightPrefixLabel;
    private carassius.GUI.IntegerTextField uxMinAquariumLengthIntegerTextField;
    private javax.swing.JLabel uxMinAquariumLengthPrefixLabel;
    private carassius.GUI.IntegerTextField uxMinAquariumLitersPerFishIntegerTextField;
    private javax.swing.JLabel uxMinAquariumLitersPerFishPrefixLabel;
    private javax.swing.JPanel uxMinAquariumSizePanel;
    private carassius.GUI.IntegerTextField uxMinTemperatureIntegerTextField;
    private javax.swing.JPanel uxOptimalWaterConditionsPanel;
    private javax.swing.JScrollPane uxOptimalWaterValuesScrollPane;
    private carassius.GUI.WaterValuesTable uxOptimalWaterValuesTable;
    private javax.swing.JLabel uxOriginPrefixLabel;
    private javax.swing.JTextField uxOriginTextField;
    private carassius.GUI.PicturePanel uxPicturePanel;
    private javax.swing.JButton uxSaveButton;
    private javax.swing.JCheckBox uxSchoolingCheckBox;
    private javax.swing.JLabel uxScientificNamePrefixLabel;
    private javax.swing.JTextField uxScientificNameTextField;
    private javax.swing.JLabel uxTemperatureLabel;
    private javax.swing.JPanel uxTemperaturePanel;
    private javax.swing.JLabel uxTemperaturePrefixLabel;
    // End of variables declaration//GEN-END:variables
}
